﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using PomodoroSuite.EntidadesNegocio;
using PomodoroSuite.FrontEnd.Win.Properties;

namespace PomodoroSuite.FrontEnd.Win
{
    public partial class frmConfiguracion : KryptonForm
    {
        #region " Construtor "

        public frmConfiguracion()
        {
            InitializeComponent();
        }

        #endregion

        #region " Eventos Formulario "

        private void frmConfiguracion_Load(object sender, EventArgs e)
        {
            List<EntidadGenerica> idiomas = new List<EntidadGenerica>();
            idiomas.Add(new EntidadGenerica("Español", "es-ES"));
            idiomas.Add(new EntidadGenerica("English", "en-US"));
            cmbIdioma.DataSource = idiomas;

            trackPomodoro.Value = int.Parse(ConfigurationManager.AppSettings["TiempoPomodoro"]);
            lblTiempoPomodoro.Text = trackPomodoro.Value.ToString();
            chkControlarDescansos.Checked = bool.Parse(ConfigurationManager.AppSettings["ControlarDescansos"]);
            trackDescansoCorto.Value = int.Parse(ConfigurationManager.AppSettings["TiempoDescansoCorto"]);
            lblDescansoCorto.Text = trackDescansoCorto.Value.ToString();
            trackDescansoLargo.Value = int.Parse(ConfigurationManager.AppSettings["TiempoDescansoLargo"]);
            lblDescansoLargo.Text = trackDescansoLargo.Value.ToString();
            cmbIdioma.SelectedValue = ConfigurationManager.AppSettings["Idioma"];
        }

        private void chkControlarDescansos_CheckedChanged(object sender, EventArgs e)
        {
            trackDescansoCorto.Enabled = chkControlarDescansos.Checked;
            trackDescansoLargo.Enabled = chkControlarDescansos.Checked;
        }

        private void trackPomodoro_Scroll(object sender, EventArgs e)
        {
            lblTiempoPomodoro.Text = trackPomodoro.Value.ToString();
        }

        private void trackDescansoCorto_Scroll(object sender, EventArgs e)
        {
            lblDescansoCorto.Text = trackDescansoCorto.Value.ToString();
        }

        private void trackDescansoLargo_Scroll(object sender, EventArgs e)
        {
            lblDescansoLargo.Text = trackDescansoLargo.Value.ToString();
        }

        private void btnGrabar_Click(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config.AppSettings.Settings["TiempoPomodoro"].Value = trackPomodoro.Value.ToString();
            config.AppSettings.Settings["ControlarDescansos"].Value = chkControlarDescansos.Checked.ToString();
            config.AppSettings.Settings["TiempoDescansoCorto"].Value = trackDescansoCorto.Value.ToString();
            config.AppSettings.Settings["TiempoDescansoLargo"].Value = trackDescansoLargo.Value.ToString();
            if (cmbIdioma.SelectedValue.ToString() != config.AppSettings.Settings["Idioma"].Value)
            {
                MessageBox.Show(Resources.frmConfiguracion_ReiniciarAplicacion, Resources.comun_Informacion, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            config.AppSettings.Settings["Idioma"].Value = cmbIdioma.SelectedValue.ToString();
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            Close();
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            Close();
        }

        #endregion
    }
}
